package com.k12.admim.controller.school;

import cn.dev33.satoken.stp.StpUtil;
import com.k12.admim.model.vo.edu.PersonInfoVo;
import com.k12.admim.model.vo.sys.SysUserVo;
import com.k12.admim.rpc.EduManageFeignClient;
import com.k12.admim.rpc.UserFeignClient;
import com.k12.admim.utils.RequestContextUtil;
import com.k12.common.model.resp.R;
import com.k12.common.util.PageUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("campus/person")
public class PersonInfoController {

    @Resource
    private EduManageFeignClient eduManageFeignClient;

    @Resource
    private UserFeignClient userFeignClient;


    @GetMapping("/list")
    public R list(PersonInfoVo personInfoVo) {
        String tenantId = RequestContextUtil.getTenantId();
        if (tenantId == null){
            return R.failed("非法登录");
        }
        Long schoolId = getSchoolId(tenantId);
        if (schoolId != null){
            personInfoVo.setSchoolId(schoolId);
        }
        List<PersonInfoVo> list = eduManageFeignClient.selectAllPerson(personInfoVo);
        PageUtil.PageResult<PersonInfoVo> result = PageUtil.paginate(list, personInfoVo.getPageNum(), personInfoVo.getPageSize());
        return R.success(result);
    }



    public Long getSchoolId(String tenantId){
        Long loginId = (Long) StpUtil.getLoginId();
        SysUserVo sysUserVo = userFeignClient.selectUserById(tenantId, loginId);
        if (sysUserVo.getSchoolId() == null || sysUserVo.getSchoolId() == 0){
            return null;
        }
        return sysUserVo.getSchoolId();
    }
}
